#include <rid.h>
#include <stdio.h>
#include <stdint.h>

#define STYLE_9
#include <unfy/unfy.h>
#include <unfy/unfp.h>

#include "../rls.h"
#include "../rlsp.h"
#include "../rlsr.h"

/* var_________________ */
static const Rid var_rid = { 0x60, 0xd4, 0xdb, 0x94, 0xfb, 0xe0, 0x0e, 0x10,
			     0xfb, 0xe0, 0x0e, 0x10, 0xfb, 0xe0, 0x0e, 0x10 };

Unfy_stat
print_act(Rls_mode mode, Rls_srch *srch, void *extra)
{
	(void) extra;

	if (mode == RLS_RUN) {
		unfp_term_print(unfy_bind_get(srch->rbind, var_rid));
		printf("\n");
	}

	return UNFY_YES;
}

int
add_print_act(Rls_rule **rule, Rls_recycle *rec)
{
	size_t pos = 0;
	const char *error;
	Unfy_term *head;

	head = unfp_term_parse("(const print_______________"
			       " var var_________________)",
			       &pos, &error, &rec->unfy);

	if (!head)
		return -1;

	if (rls_rule_add_actable(rule, NULL, head, print_act, NULL, rec) < 0) {
		unfy_recycle_term(&rec->unfy, head);
		return -1;
	}

	return 0;
}

int
main(int argc, char *argv[])
{
	Rls_recycle rec;
	Rls_rule *rules = NULL;
	Rlsr_lsnr lsnr;

	rls_recycle_init(&rec);

	if (add_print_act(&rules, &rec) < 0)
		printf("Failed to add print actable rule\n");

	rlsr_lsnr_init(&lsnr, NULL, NULL, NULL, NULL, NULL, NULL, 0);
	rlsr_repl(&rules, &lsnr, NULL, &rec);
	rls_recycle_rule(&rec, rules, NULL, NULL);
	rls_recycle_empty(&rec);
	return 0;
}
